"""
@Description :   REC 任务评价指标 Handler
@Author      :   tqychy 
@Time        :   2025/08/23 17:14:50
"""
import sys

sys.path.append("./metrics")

from tools import *


class RecMetricsHandler:
    def __init__(self, *args):
        self.cfg, self.logger = args
        self._iou = AverageMeter()
        self._ap50 = AverageMeter()
        self._ap75 = AverageMeter()
        self._ap90 = AverageMeter()

    def update(self, pred_bbox, gt_bbox):
        iou_score = iou(pred_bbox, gt_bbox) if pred_bbox is not None else 0.
        ap50 = (iou_score >= 0.5)
        ap75 = (iou_score >= 0.75)
        ap90 = (iou_score >= 0.9)

        self._iou.update(iou_score)
        self._ap50.update(ap50)
        self._ap75.update(ap75)
        self._ap90.update(ap90)
        return iou_score, ap50, ap75, ap90

    def miou(self):
        return self._iou.avg
    
    def map(self):
        return self._ap50.avg, self._ap75.avg, self._ap90.avg
    
    